package test;

import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;

import opennlp.tools.util.Span;

public class MyTik extends Tokenizer {
	private final CharTermAttribute termAtt = (CharTermAttribute) this.addAttribute(CharTermAttribute.class);
	private final OffsetAttribute offsetAtt = (OffsetAttribute) this.addAttribute(OffsetAttribute.class);
	private final PositionIncrementAttribute posIncrAtt = (PositionIncrementAttribute) this
			.addAttribute(PositionIncrementAttribute.class);
	int starta = 0;
	int enda = 0;

	@Override
	public boolean incrementToken() throws IOException {
		// TODO Auto-generated method stub
		this.clearAttributes();
		BufferedReader br = new BufferedReader(this.input);
		StringBuffer sb = new StringBuffer("");
		String line = br.readLine();
		while (line != null) {
			sb.append(line);
			line = br.readLine();
		}
		this.enda += sb.length();
		if (sb.length() > 0) {
//			if (this.termAtt.length() == 0) {
//				while (true) {
//					System.out.println(this.enda+"::::"+this.termAtt.length());
//					if (this.enda > this.termAtt.length()-1) {
//						
//						char[] resizeBuffer = this.termAtt.resizeBuffer(2+this.termAtt.length());
//						this.termAtt.setLength(resizeBuffer.length);
//					} else {
//						break;
//					}
//				}
//			} else if (this.termAtt.length() > 0) {
//			
//			}
			System.out.println("length:"+this.termAtt.length());
			System.out.println(this.termAtt.buffer().length);
			System.out.println(sb.toString());
			
			posIncrAtt.setPositionIncrement(1);
			this.termAtt.copyBuffer(sb.toString().toCharArray(), this.starta, sb.length());
			
			posIncrAtt.setPositionIncrement(2);
			this.termAtt.copyBuffer(sb.toString().substring(0, 1).toCharArray(), this.starta,1);
			
//			CharTermAttribute t = (CharTermAttribute) this.addAttribute(CharTermAttribute.class);
//			t.copyBuffer(sb.toString().substring(0,1).toCharArray(), this.starta, 1);

			//this.termAtt.copyBuffer(sb.toString().substring(0,1).toCharArray(), this.starta, 1);
			
			this.offsetAtt.setOffset(this.starta, this.enda);
			System.out.println(this.termAtt.buffer());

			//this.starta += sb.length();
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void reset() throws IOException {
		// TODO Auto-generated method stub
		super.reset();
		// BufferedReader br = new BufferedReader(this.input);
		// String line = br.readLine();
		// while (line != null) {
		// System.out.println(line);
		// li.add(line);
		// line = br.readLine();
		// }
		System.out.println("----------------------------------------------------");
	}
}
